﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminTravelProductController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TravelProductModel model = new TravelProductModel();

                foreach (var travelProduct in db.EntityTravelProductions.Where(o => o.IdTravelInfo == 1))
                {
                    model.listTravelProduct.Add(new TravelProduct()
                    {
                        Id = travelProduct.Id,
                        IdTravelInfo = travelProduct.IdTravelInfo,
                        Name = travelProduct.Name,
                        Description = travelProduct.Description,
                        About = travelProduct.About,
                        Image = travelProduct.Image,
                        Position = travelProduct.Position,
                        //<!-- SEO -->
                        Titletag = travelProduct.Titletag,
                        Meta_description = travelProduct.Meta_description,
                        Meta_keyword = travelProduct.Meta_keyword
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();
                EntityTravelProduction item = db.EntityTravelProductions.Where(p => p.Id == id).Single();
                if (item.Image != null)
                {
                    string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/travelproductions/"), item.Image); // file hinh cu
                    System.IO.File.Delete(fileToDelete);
                }

                db.EntityTravelProductions.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";
            }
            catch
            {
                return "Error";

            }

        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                TravelProduct model = new TravelProduct();
                return PartialView(model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(TravelProduct model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTravelProduction travelProduct = new EntityTravelProduction();
                travelProduct.IdTravelInfo = 1;
                travelProduct.Name = model.Name;
                travelProduct.Description = model.Description;
                travelProduct.About = model.About;
                travelProduct.Position = model.Position;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/travelproductions/"), encodestring);
                    Image.SaveAs(path);

                    travelProduct.Image = encodestring;
                }
                //<!-- SEO -->
                travelProduct.Titletag = model.Titletag;
                travelProduct.Meta_description = model.Meta_description;
                travelProduct.Meta_keyword = model.Meta_keyword;

                db.EntityTravelProductions.InsertOnSubmit(travelProduct);
                db.SubmitChanges();

                return RedirectToAction("Index", "AdminTravelProduct");
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int idTravelProduct)
        {
            dbVietPremierDataContext db = new dbVietPremierDataContext();

            ViewBag.Name = (from p in db.EntityTravelProductions
                            where p.Id == idTravelProduct
                            select p).SingleOrDefault().Name;
            return PartialView();
        }

        [HttpPost]
        public JsonResult GetDetail(int idTravelProduct)
        {
            TravelProduct model = new TravelProduct();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == idTravelProduct);

                if (travelProduct != null)
                {
                    model.Id = travelProduct.Id;
                    model.IdTravelInfo = travelProduct.IdTravelInfo;
                    model.Name = travelProduct.Name;
                    model.Description = travelProduct.Description;
                    model.About = travelProduct.About;
                    model.Image = travelProduct.Image;
                    model.Position = travelProduct.Position;
                    //<!--SEO -->
                    model.Titletag = travelProduct.Titletag;
                    model.Meta_description = travelProduct.Meta_description;
                    model.Meta_keyword = travelProduct.Meta_keyword;
                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int Id, int IdTravelInfo, int Position, HttpPostedFileBase Image, string Name, string Description, string About, string Titletag, string Meta_description, string Meta_keyword)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntityTravelProduction travelProduct = db.EntityTravelProductions.Where(p => p.Id == Id).FirstOrDefault();
                travelProduct.Position = Position;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (travelProduct.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/travelproductions/"), travelProduct.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/travelproductions/"), encodestring);
                    Image.SaveAs(path);

                    travelProduct.Image = encodestring;
                }
                travelProduct.Name = Name;
                travelProduct.Description = Description;
                travelProduct.About = About;
               
                //<!--SEO -->
                travelProduct.Titletag = Titletag;
                travelProduct.Meta_description = Meta_description;
                travelProduct.Meta_keyword = Meta_keyword;

                db.SubmitChanges();
                return RedirectToAction("Index", "AdminTravelProduct");
            }
        }
        #endregion //edit

    }
}
